#include <QApplication>
#include <QCoreApplication>
#include "ui_mainwidget.h"
#include "usermanager.h"
#include <QThread>
#include "loadingwidget.h"
#include "QDebug"
#include "mainwidget.h"
#include "QScreen"

//启动初始化管理员
void initAdmin(){
    UserManager userManager;
    // 检查用户是否存在
    if (!userManager.checkUserExists("admin")) {
        userManager.createAdmin();
    }
}

//周期检测电池信息
void batteryCheckThread(MainWidget* m){
    while(true){
        int usage=5;//接入电池硬件
        m->on_progressBar_valueChanged(usage);
        std::this_thread::sleep_for((std::chrono::seconds(10)));//延迟10s
    }
}

//周期检测网络连接
void networkCheckThread(MainWidget* m){
    while(true){
        //网络检测并发出信号
        QNetworkConfigurationManager mgr;
        if(mgr.isOnline()){
            m->wifishow(true);
        }else{
            m->wifishow(false);
            }
        std::this_thread::sleep_for((std::chrono::seconds(10)));//延迟10s
    }
}

//创建多线程进行检测
void threadjoin(MainWidget* m){
    std::thread wifiCheck(networkCheckThread,m);//创建wifi检测线程
    std::thread batteryCheck(batteryCheckThread,m);//创建电池检测线程
    wifiCheck.detach();
    batteryCheck.detach();
}

int main(int argc, char *argv[])
{
    // 设置 QT_SCALE_FACTOR 环境变量
    qputenv("QT_SCALE_FACTOR", "2.25");
    QApplication a(argc, argv);
    LoadingWidget test;
    test.showFullScreen();
    initAdmin();
    MainWidget m;
    threadjoin(&m);
    m.showFullScreen();
    return a.exec();
}

